<?php
include_once 'admin-functions.php';
include_once '../functions.php';
if (isset($_SESSION['logged_in'])) {} else {header('Location: page.php');}
?>
<script type="text/javascript">
var xmlHttp
function saveTitle() {
	if (document.getElementById("saveButton").style.color == "black") {
		xmlHttp=GetXmlHttpObject()
		
		if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request")
		return
		} 
		var url="autosave.php"
		var title = document.getElementById("item_a").value
		var content = document.getElementById("item_c").value
		xmlHttp.onreadystatechange=stateChanged 
		xmlHttp.open("POST",url,true)
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlHttp.send("title="+title+"&content="+content)
	}
} 

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{ 
			document.getElementById("saveButton").style.color = "gray"		
} 
} 

function GetXmlHttpObject()
{ 
var objXMLHttp=null
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
}


var secs = 3
var timerID = null
var timerRunning = true
var delay = 1000

function InitializeTimer()
{
    // Set the length of the timer, in seconds
    secs = 10
    StopTheClock()
    StartTheTimer()
}

function StopTheClock()
{
    if(timerRunning)
        clearTimeout(timerID)
    timerRunning = false
}

function StartTheTimer()
{
    if (secs==0)
    {
        StopTheClock()
        saveTitle()
        InitializeTimer()
		StartTheTimer()
    }
    else
    {
        self.status = secs
        secs = secs - 1
        timerRunning = true
        timerID = self.setTimeout("StartTheTimer()", delay)
    }
}

function unsaved() {
	document.getElementById("saveButton").style.color = "black"
}

</script>
<?php 

if (isset($_POST['post']) == 0 && isset($_POST['title']) == 0)  {
	echo '
		<div style="float:right;background:lightgrey;padding:5px;margin-top:100px;width:380px;">
			<p>
			<ul>
				<li><em>*emphasized*</em></li>
				<li><strong>**strong**</strong></li>
				<li>headline1, h2, h3: =, ==, ===</li>
				<li>[[sidenote]]</li>
				<li>links: [url text]</li>
				<li>images: [url]</li>
				<li>lists can be done with * in front of items</li>
				<li>numbered lists via # in front of items</li>
				
				</p>
		</div>
		<h2>Compose</h2>
		<form action="page.php?p=add" method="post">
					<input name="creationdate" type="hidden"/>
			<label for="item_a">Title:</label>
			<p class="input">
				<input onkeyup="unsaved()" name="title" id="item_a" style="width:30em;" />
			</p>
			
			<label for="item_c">Content:</label>
			<p class="text">
				<textarea onkeyup="unsaved()" name="post" id="item_c" rows="20" cols="70"></textarea>
			</p>

			<p class="button">	
				<button name="save" id="saveButton" type="button" onclick="saveTitle()">Save</button>				
				<button name="submit" type="submit">Post</button>							
			</p>			
		</form>

';}

/* BEGIN THE ADDING FUNCTION */
elseif (isset($_POST['post']) == 1 && isset($_POST['title']) == 1 && isset($_SESSION['logged_in'])) {
	$title = $_POST['title'];
	$post = $_POST['post'];
	add_post($title, $post, $_SERVER['REQUEST_TIME'], $_SERVER['REQUEST_TIME'], $_SERVER['REQUEST_TIME'], $_SESSION['account_name'], $blog_db);
}

// bring in the grande finale
include_once 'theme/top.php';
?>


